package com.example.safeassistan.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.safeassistan.R;


public class MyRelativeLayout extends RelativeLayout implements View.OnTouchListener {

    private float WidthFloat;
    private float HighFloat;
    private Boolean ChangeWidth;

    public MyRelativeLayout(Context context) {
        super(context);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRelativeLayout);
        WidthFloat = typedArray.getFloat(R.styleable.MyRelativeLayout_Width_Ratio, 0);
        HighFloat = typedArray.getFloat(R.styleable.MyRelativeLayout_High_Ratio, 0);
        ChangeWidth = typedArray.getBoolean(R.styleable.MyRelativeLayout_Change_Width, false);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRelativeLayout, defStyleAttr, 0);

        WidthFloat = typedArray.getFloat(R.styleable.MyRelativeLayout_Width_Ratio, 0);
        HighFloat = typedArray.getFloat(R.styleable.MyRelativeLayout_High_Ratio, 0);
        ChangeWidth = typedArray.getBoolean(R.styleable.MyRelativeLayout_Change_Width, false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (WidthFloat != 0 && HighFloat != 0) {
            if (ChangeWidth) {
                widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (MeasureSpec.getSize(heightMeasureSpec) / HighFloat * WidthFloat), MeasureSpec.EXACTLY);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            } else {
                heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (MeasureSpec.getSize(widthMeasureSpec) / WidthFloat * HighFloat), MeasureSpec.EXACTLY);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        } else {
            //必须调用下面的两个方法之一完成onMeasure方法的重写，否则会报错
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    private float downX;
    private float downY;


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    downY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    if (xDistance != 0 && yDistance != 0) {
                        int l = (int) (getLeft() + xDistance);
                        int r = (int) (getRight() + xDistance);
                        int t = (int) (getTop() + yDistance);
                        int b = (int) (getBottom() + yDistance);
                        this.layout(l, t, r, b);

                    }
                    break;
                case MotionEvent.ACTION_UP:
                    setPressed(false);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    setPressed(false);
                    break;
            }
            return true;
        }
        return false;
    }
}
